package com.java.base.exercise.current;

/**
 * @Author RenPu
 * @Date 2021/8/31 14:41
 * @Version 1.0
 * @Description: 俩个线程实现，交替输出奇数，偶数的打印 从0-100  实现方案2
 * <p>
 * 偶数:0
 * 奇数:1
 * 偶数:2
 * 奇数:3
 * 偶数:4
 * 奇数:5
 * 偶数:6
 **/
public class Thread_002 {

    private static final Object lock = new Object();

    private static int count = 0;

    public static void main(String[] args) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (count < 100) {
                    synchronized (lock) {
                        if ((count & 1) == 0) {
                            System.out.println(Thread.currentThread().getName() + ":" + count++);
                        }
                    }
                }

            }
        }, "偶数").start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                while (count < 100) {
                    synchronized (lock) {
                        if ((count & 1) == 1) {
                            System.out.println(Thread.currentThread().getName() + ":" + count++);
                        }
                    }
                }

            }
        }, "奇数").start();


    }


}
